package com.xinxin.facelinker.adapter;

import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.easemob.chatuidemo.R;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.xinxin.facelinker.widget.WrapHeightGridView;

/**
 * Created by gengliang on 2015/8/19.
 */
public class GridViewMotionPhotoAdapter extends BaseAdapter {

    private Context context;
    private String[] motion_photo_url;
    private ImageLoader imageLoader= ImageLoader.getInstance();

    public Context getContext() {
        return context;
    }

    public void setContext(Context context) {
        this.context = context;
    }

    public String[] getMotion_photo_url() {
        return motion_photo_url;
    }

    public void setMotion_photo_url(String[] motion_photo_url) {
        this.motion_photo_url = motion_photo_url;
    }

    public GridViewMotionPhotoAdapter(String[] motion_photo_url, Context context) {

        this.motion_photo_url = motion_photo_url;
        this.context = context;
    }

    @Override
    public int getCount() {
        return motion_photo_url.length;
    }

    @Override
    public Object getItem(int position) {
        return motion_photo_url[position];
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {


        RelativeLayout relativeLayout;

        if(convertView!=null){
            relativeLayout= (RelativeLayout) convertView;
        }else{
            relativeLayout= (RelativeLayout) LayoutInflater.from(getContext()).inflate(R.layout.layout_gridview_motion_photo,null);
        }

        ImageView ivMotionPhotoImage= (ImageView) relativeLayout.findViewById(R.id.ivMotionPhotoImage);
        ImageView ivMotionPhotoImageDelete= (ImageView) relativeLayout.findViewById(R.id.ivMotionPhotoImageDelete);

        GridView gv= (WrapHeightGridView) parent;
        int horizontalSpacing=gv.getHorizontalSpacing();
        int numColumns=gv.getNumColumns();
        int itemWidth=(gv.getWidth()-(numColumns-1)*horizontalSpacing-gv.getPaddingLeft()-gv.getPaddingRight())/numColumns;

        RelativeLayout.LayoutParams params=new RelativeLayout.LayoutParams(itemWidth,itemWidth);
        ivMotionPhotoImage.setLayoutParams(params);

        imageLoader.displayImage(motion_photo_url[position],ivMotionPhotoImage);

        return relativeLayout;
    }
}
